<?php

/**
 * building model
 * 
 * @author HungPV <phamvanhung0818@gmail.com>
 * @package building
 * @since 1.0
 */
class building extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    function get_count() {
        return $this->db->count_all('building');
    }
    
    function get($id) {
        $query = $this->db->get_where('building', array('building_id' => $id));
        $result = $query->result();

        if (count($result) > 0) {
            return $result[0];
        } else {
            return NULL;
        }
    }
    
    function get_building_spot_info($id) {
        $this->db->select('building_id, building_name');
        $this->db->from('building');
        $this->db->where('building_id', $id);
        $query = $this->db->get();
        $result = $query->result();

        if (count($result) > 0) {
            return $result[0];
        } else {
            return NULL;
        }
    }
    
    function get_building($id) {
        $query = $this->db->get_where('building', array('building_id' => $id));
        $result = $query->result_array();
        if (count($result) > 0) {
            return $result[0];
        } else {
            return FALSE;
        }
    }
    
    function get_buildings($ofset = -1, $limit = 0) {
        if ($ofset == -1) {
            $query = $this->db->get_where('building');
        } else {
            $query = $this->db->get('building', $limit, $ofset);
        }
        return $query->result();
    }
    
    function get_buildings_by_cat($cat_id,$ofset = -1, $limit = 0) {
        if ($ofset == -1) {
            $this->db->select('building_id,building_name,building_info,building_category_id,related_building');
            $this->db->from('building');
            $this->db->where('building_category_id',$cat_id);            
            $query = $this->db->get();            
        } else {
            $this->db->select('building_id,building_name,building_info,building_category_id,related_building');
            $this->db->from('building');
            $this->db->where('building_category_id',$cat_id);         
            $this->db->limit($limit,$ofset);
            $query = $this->db->get();
        }
        return $query->result();
    }
    
    function get_building_array_by_cat($cat_id,$ofset = -1, $limit = 0) {
        if ($ofset == -1) {
            $this->db->select('building_id,building_name,building_info,building_category_id,related_building');
            $this->db->from('building');
            $this->db->where('building_category_id',$cat_id);            
            $query = $this->db->get();            
        } else {
            $this->db->select('building_id,building_name,building_info,building_category_id,related_building');
            $this->db->from('building');
            $this->db->where('building_category_id',$cat_id);         
            $this->db->limit($limit,$ofset);
            $query = $this->db->get();
        }
        return $query->result_array();
    }

    function add($name, $description, $cat, $related = "") {
        $data = array(
            'building_name' => $name,
            'building_info' => $description,
            'building_category_id' => $cat,
            'related_building' => $related
        );

        $this->db->insert('building', $data);
        return $this->db->insert_id();
    }
    
    function edit($id, $name, $description,$cat, $related = "") {
        $data = array(
            'building_name' => $name,
            'building_info' => $description,
            'building_category_id' => $cat,
            'related_building' => $related
        );
        $this->db->where('building_id', $id);
        $this->db->update('building', $data);
    }
    
    function delete($id) {
        $this->db->delete('building', array('building_id' => $id));
    }

}

?>
